package com.woniuxy.educationplatform.mapper;

import com.woniuxy.educationplatform.bean.AdminBean;
import com.woniuxy.educationplatform.bean.RoleBean;
import org.apache.ibatis.annotations.*;
import org.apache.ibatis.mapping.FetchType;

import java.util.List;

/**
 * @author unreleased
 * @date 2020/12/10 10:06
 */
@Mapper
public interface AdminMapper {
    /**
     * 根据具体id查询管理员一些简单信息
     *
     * @param id 根据id查询管理员
     * @return 返回一个管理员对象
     */
    @Select("select id,admin_name,login_name,gender,email,admin_desc,telephone from admin_info where status=0 and id = #{id}")
    AdminBean findAdminById(Integer id);

    /**
     * 根据shiro中的principle查询具体信息
     *
     * @param loginName 登录名
     * @return Admin对象
     */
    @Results({
            //甚至连主键都可以省略
            @Result(id = true, property = "id", column = "id", javaType = Integer.class),
            @Result(property = "roleBean", column = "fk_role_id", javaType = RoleBean.class, one =
            @One(select = "com.woniuxy.educationplatform.mapper.RoleMapper.findRoleById", fetchType = FetchType.LAZY))
    })
    @Select("select id,admin_name,login_name,gender,email,admin_desc,telephone,fk_role_id from admin_info where login_name = #{loginName} and status=0")
    AdminBean findAdminByLoginName(String loginName);

    /**
     * 根据登录名来修改管理员信息
     * 用于管理员修改个人信息
     *
     * @param adminBean 修改的数据
     * @return 数据库受影响条数 判断是否修改成功
     */
    @Update("<script>" +
            "update admin_info" +
            "<set>" +
            "<if test='a.adminName != null'>" +
            " admin_name = #{a.adminName}," +
            "</if>" +
            "<if test='a.gender != null'>" +
            " gender = #{a.gender}," +
            "</if>" +
            "<if test='a.email != null'>" +
            " email = #{a.email}," +
            "</if>" +
            "<if test='a.telephone != null'>" +
            " telephone = #{a.telephone}," +
            "</if>" +
            "<if test='a.adminDesc != null'>" +
            " admin_desc = #{a.adminDesc}," +
            "</if>" +
            "</set>" +
            "where login_name = #{a.loginName}" +
            "</script>")
    int updateAdminByLoginName(@Param("a") AdminBean adminBean);

    /**
     * 根据管理员登录名修改密码
     * @param loginName 登录名
     * @param newPassword 新密码 通过md算法加密设置
     * @param oldPassword 原密码
     * @return 是否修改成功
     */
    @Update("update admin_info set password = #{newPassword} where login_name = #{loginName} and password=#{oldPassword}")
    int updateAdminPassword(@Param("loginName") String loginName,@Param("oldPassword") String oldPassword,@Param("newPassword") String newPassword);

    /**
     * 分页查询
     * @param adminBean 含有参数的adminBean
     * @return List集合
     */
    List<AdminBean> findAllByParams(@Param("a") AdminBean adminBean);

    /**
     * 根据登录名来修改管理员信息
     * 用于超级管理员修改信息
     * 后期可用于修改管理员的登录名
     * @param adminBean 修改的数据
     * @return 数据库受影响条数 判断是否修改成功
     */
    @Update("<script>" +
            "update admin_info" +
            "<set>" +
            "<if test='a.adminName != null'>" +
            " admin_name = #{a.adminName}," +
            "</if>" +
            "<if test='a.gender != null'>" +
            " gender = #{a.gender}," +
            "</if>" +
            "<if test='a.email != null'>" +
            " email = #{a.email}," +
            "</if>" +
            "<if test='a.roleBean != null'>" +
            " fk_role_id = #{a.roleBean.id}," +
            "</if>" +
            "<if test='a.telephone != null'>" +
            " telephone = #{a.telephone}," +
            "</if>" +
            "<if test='a.password != null'>" +
            " password = #{a.password}," +
            "</if>" +"<if test='a.status != null'>" +
            " status = #{a.status}," +
            "</if>" +
            "<if test='a.loginName != null'>" +
            " login_name = #{a.loginName}," +
            "</if>" +
            "<if test='a.adminDesc != null'>" +
            " admin_desc = #{a.adminDesc}," +
            "</if>" +
            "</set>" +
            "where id = #{a.id}" +
            "</script>")
    int updateAdminById(@Param("a") AdminBean adminBean);

    /**
     * 根据管理员ID删除对应的管理员
     * @param id 前端对应id
     * @return 返回收影响的条数
     */
    @Delete("delete from admin_info where id = #{id}")
    int delAdminById(Integer id);

    /**
     * 用于检查登录名是否重复
     * @param loginName 登录名
     * @return adminBean对象
     */
    @Select("select id,login_name from admin_info where login_name = #{loginName}")
    AdminBean checkByLoginName(String loginName);

    /**
     * 用于检查登录名是否重复
     * @param id id
     * @return adminBean对象
     */
    @Select("select id,login_name from admin_info where id = #{id}")
    AdminBean checkById(Integer id);

    /**
     * 新增管理员
     * @param adminBean 携带信息的adminBean对象
     * @return 是否添加成功
     */
    @Insert("insert into admin_info (admin_name,login_name,password,fk_role_id,create_time) values (#{a.adminName},#{a.loginName},#{a.password},#{a.roleBean.id},NOW())")
    int saveAdmin(@Param("a") AdminBean adminBean);

    /**
     * 新增管理员
     * BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG BUG
     * 此方用于批量添加管理员 由于从excel中暂时无法将fk_role_id封装到Bean中  所以数据库的fk_role_id暂时默认为2
     * @param adminBean 携带信息的adminBean
     * @return 影响行数;
     */
    @Insert("insert into admin_info (admin_name,login_name,create_time,gender,telephone,email,admin_desc,status) values (#{a.adminName},#{a.loginName},#{a.createTime},#{a.gender},#{a.telephone},#{a.email},#{a.adminDesc},#{a.status})")
    int addAdmin(@Param("a") AdminBean adminBean);
}
